<?php


namespace app\admin\controller;


use app\model\Admin;
use think\Controller;
use think\facade\Session;

class Login extends Controller
{
    /**
     * 登录
     * @return array|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function login()
    {
        if(request()->post()){
            $params = request()->param();
            if(empty($params['account']) || empty($params['password'])){
                return errorMsg("账号或密码不能为空");
            }
            $account = Admin::where('account',$params['account'])->where("password",md5($params['password']))->find();
            if(empty($account)) {
                return errorMsg("账号或密码不正确");
            }
            session("admin_id",$account['id']);
            session("last_login_time",$account['login_time']);
            Admin::where("id",$account['id'])->update(['login_time' => date("Y-m-d H:i:s",time()),'login_ip' => request()->ip(),'login_num'=>$account['login_num']+1]);
            return successMsg("登录成功");
        }
        return view("login");
    }

    /**
     * 退出登陆
     */
    public function logout(){
        Session::clear();
        $this->success("退出成功","/admin/login/login");
    }
}